package com.itic.mall.mobile.uc.feedback;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.utils.StringHelper;
import com.itic.mall.mobile.base.BaseMobileInterface;
import com.itic.mall.system.user.persistence.model.MallUser;
import com.itic.mall.system.userfeedback.persistence.model.UserFeedback;
import com.itic.mall.system.userfeedback.service.UserFeedbackService;


/**
 * 用户意见反馈接口类
 * @author 许小宝
 * 2017年07月26日  14时11分
 */
@Controller
@RequestMapping(value="/mobile/uc/feedback")
public class MallUcFeedbackInterface
     extends BaseMobileInterface {
   
	@Autowired
	private UserFeedbackService userFeedbackService;
	
    /**
     * 用户添加意见反馈信息(此方法涉及到中文，必须用POST方式提交)
     * @return 
     */
    @RequestMapping(method= RequestMethod.POST,value="/add")
    @ResponseBody
    public Map<String,Object> add( HttpServletRequest request, HttpServletResponse response, String token, String content ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		//判断必要的参数
    		if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(content) ){
    			MallUser user = getMallUserByToken(token);
    			if( null != user ){
    				UserFeedback feedback = new UserFeedback();
    				feedback.setUserId(user.getId());
    				feedback.setContent(content);
    				String addResult = userFeedbackService.add(feedback);
    				if( SUCCESS.equals(addResult) ){
    					result = SUCCESS;
            			errorCode = SUCCESS_ERROR_CODE;
                    	errorMsg = SUCCESS_ERROR_MSG; 
    				}
    			}else{
    				result = LOGIN;
        			errorCode = LOGIN_ERROR_CODE;
                	errorMsg = LOGIN_ERROR_MSG;  	
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	//log(e);
        	logException("MallUcFeedbackInterface", "add", e.toString(),"添加意见反馈",e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
    
    
}